import Router from 'koa-router'
import request from '@/request'
import { writeFileSync } from 'fs'
import logger from '@/utils/logger'

const test = new Router()

test.get('/', async (ctx, next) => {
  ctx.response.status = 200
  ctx.response.body = JSON.stringify({ message: '请求成功', code: '10', data: ['测试数据'] })
  await next()
})

test.get('/html', async (ctx, next) => {
  ctx.response.status = 200
  const res = await request<string>('https://api.pengrengui.com')
  ctx.response.body = res
  await next()
})

test.get('/list', async (ctx, next) => {
  ctx.response.status = 200
  const res = await request<{
    result: {
      songs: any[]
    }
  }>('https://api.pengrengui.com/cloudsearch', {
    data: {
      keywords: '海阔天空'
    }
  })
  ctx.response.body = res.result?.songs
  await next()
})

test.get('/file', async (ctx, next) => {
  console.log('收到请求 :>> ', ctx)
  logger.info(ctx)
  ctx.response.status = 200
  writeFileSync('./ctx.json', JSON.stringify(ctx, null, 2))

  ctx.response.body = ctx
  await next()
})

module.exports = test
